package com.offcn.webui.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.webui.service.OrderServiceFeign;
import com.offcn.webui.vo.resp.OrderFormInfoSubmitVo;
import com.offcn.webui.vo.resp.ReturnPayConfirmVo;
import com.offcn.webui.vo.resp.TOrder;
import com.offcn.webui.vo.resp.UserRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/01/31/21:07
 * @Description:
 */
@Controller
@Slf4j
public class OrderController {

    @Autowired
    private OrderServiceFeign orderServiceFeign;


    /**
     * 保存订单
     *
     * @param vo
     * @param session
     * @return
     */
    @RequestMapping("/order/save")
    public String orderPay(OrderFormInfoSubmitVo vo, HttpSession session) {
        UserRespVo userRespVo = (UserRespVo) session.getAttribute("sessionMember");
        if (userRespVo == null) {
            return "redirect:/login.html";
        }
        String accessToken = userRespVo.getAccessToken();
        vo.setAccessToken(accessToken);
        ReturnPayConfirmVo confirmVo = (ReturnPayConfirmVo) session.getAttribute("returnConfirmSession");
        if (confirmVo == null) {
            return "redirect:/login";
        }
        vo.setProjectid(confirmVo.getProjectId());
        vo.setReturnid(confirmVo.getId());
        vo.setRtncount(confirmVo.getNum());
        AppResponse<TOrder> order = orderServiceFeign.createOrder(vo);
        TOrder tOrder = order.getData();
        //下单成功，打印相关信息待处理
        log.info("orderNum:{}", tOrder.getOrdernum());
        log.info("money:{}", tOrder.getMoney());
        log.info("orderName:{}", confirmVo.getProjectName());
        log.info("remark:{}", tOrder.getRemark());
        return "/member/minecrowdfunding";
    }
}
